package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 
 * http://docs.oracle.com/javase/6/docs/technotes/guides/jdbc/getstart/
 * GettingStartedTOC.fm.html
 * http://www.adictosaltrabajo.com/tutoriales/tutoriales
 * .php?pagina=tutorial_basico_jdbc
 * http://docs.oracle.com/javase/tutorial/jdbc/basics/gettingstarted.html
 * 
 * @author Mario Romero
 * 
 */

public class DbUtils {

	public static final String DERBY_EmbebedDriver = new String(
			"org.apache.derby.jdbc.EmbeddedDriver");
	public static final String DERBY_ClientDriver = new String(
			"org.apache.derby.jdbc.ClientDriver");
	private static String DERBY_DBNAME = "listaCompraMakerdb";
	public static final String DERBY_DBURL = new String(
			"jdbc:derby://localhost:1527/" + DERBY_DBNAME + ";create=true");

	// http://es.wikipedia.org/wiki/Singleton
	private static Connection connection;

	public static Connection createInstance() {
		if (connection == null) {
			try {
				Class.forName(DERBY_ClientDriver).newInstance();
				connection = DriverManager.getConnection(DERBY_DBURL);
			} catch (Exception except) {
				except.printStackTrace();
			}
		}
		return connection;
	}

	public static void shutdown() {
		try {
			if (connection != null) {
				DriverManager.getConnection(DERBY_DBURL + ";shutdown=true");
				connection.close();
			}
		} catch (SQLException sqlExcept) {

		}

	}
}